import java.awt.Color;
import java.awt.Point;

import drawing.AlgebraicLine;
import excaptions.AandBZeroException;
import ij.IJ;
import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.plugin.PlugIn;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


public class A02_Draw_Algebraic_Line implements PlugIn{

	static public double A = 0.7593;
	static public double B = -0.6507;
	static public double C = 33.7113;
	
	static public int X1 = 0;
	static public int X2 = 400;
	static public int Y1 = 0;
	static public int Y2 = 400;
	
	static public boolean ABC = true;
	static String imageTitle = "draw-algebraic-line";
	
	private AlgebraicLine al;
	
	public void run(String arg0) {
		if (!getUserInput()) {
			return;
		}
		
		if(ABC)
		{
			try {
				al = new AlgebraicLine(A, B, C);
			} catch (AandBZeroException e) {
				
				IJ.log(e.toString());
				e.printStackTrace();
			}
		}
		else
		{
			try {
				al = new AlgebraicLine(new Point(X1, Y1), new Point(X2, Y2));
			} catch (AandBZeroException e) {
				
				IJ.log(e.toString());
				e.printStackTrace();
			}
		}
				
		IJ.log("A: " + al.m_A + "\nB: " + al.m_B + "\nC: " + al.m_C);
		// create a new grayscale image
		ImageProcessor ip = new ColorProcessor(400, 400);//new ByteProcessor(400, 400);
		ip.setColor(new Color(255, 255, 255));
		ip.fillOval(-200, -200, ip.getWidth()*2, ip.getHeight()*2);
		
		//ip.drawRect(0, 0, ip.getWidth(), ip.getHeight());
							// to show white background
		
		//ip = al.draw(ip);
		
		// display the new image
		(new ImagePlus(imageTitle, al.draw(ip, Color.red))).show();
	}

	boolean getUserInput() {
		GenericDialog gd = new GenericDialog("Create Circle Test Image");
		gd.addCheckbox("ABC?", true);
		gd.addMessage("Algebraic Line:");
		gd.addNumericField("A", A, 4);
		gd.addNumericField("B", B, 4);
		gd.addNumericField("C", C, 4);
		gd.addMessage("Kxd form:");
		gd.addNumericField("Point1 x:", X1, 0);
		gd.addNumericField("Point1 y:", Y1, 0);
		gd.addNumericField("Point2 x:", X2, 0);
		gd.addNumericField("Point2 y:", Y2, 0);
		gd.showDialog();
		if (gd.wasCanceled()) {
			return false;
		}
		ABC = gd.getNextBoolean();
		A = gd.getNextNumber();
		B = gd.getNextNumber();
		C = gd.getNextNumber();
		X1 = (int) gd.getNextNumber();
		Y1 = (int) gd.getNextNumber();
		X2 = (int) gd.getNextNumber();
		Y2 = (int) gd.getNextNumber();
		return true;
	}
}
